layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#userList',
        url : '/admin/sys/user/search.do',
        page : true,
        limits : [10,15,20,25],
        limit : 20,
        id : "userListTable",
        cols : [[
            {field: 'userId', title: '用户id',  align:"center"},
            {field: 'username', title: '用户名',  align:"center"},
            {field: 'email', title: '用户邮箱',  align:'center',templet:function(d){
                return '<a class="layui-blue" href="mailto:'+d.email+'">'+d.email+'</a>';
            }},
            {field: 'status', title: '用户状态',  align:'center',templet:function(d){
                return d.status == "1" ? "正常" : "冻结";
            }},
            {field: 'mobile', title: '手机号码', align:'center'},
            {title: '操作', templet:'#userListBar',fixed:"right",align:"center"}
        ]]
    });
    $(".search_btn").on("click",function(){
            table.reload("userListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    userId: $("#userId").val(),
                    username: $("#username").val(),
                    status: $('#status').val()
                }
            })
    });

    function addUser(edit){
        var index = layui.layer.open({
            title : "添加用户",
            type : 2,
            content : edit?"userAdd.html?userId="+edit.userId:"userAdd.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find("#userId").val(edit.userId);
                    body.find("#username").val(edit.username);  //登录名
                    body.find("#email").val(edit.email);  //邮箱
                    body.find("#mobile").val(edit.mobile);
                    body.find("#status input[value="+edit.status+"]").prop("checked","checked");
                    body.find("#username").attr("disabled","disabled");
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
    }
    $("#addNews_btn").click(function(){
        addUser();
    });

    function audit(userId,status){
        $.get("/admin/sys/user/updateStatus.do",{userId:userId,status:status},function (res) {
            if(res.code==0){
                table.reload("userListTable",{
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                        userId: $("#userId").val(),
                        username: $("#username").val(),
                        status: $('#status').val()
                    }
                })
            }else{
                layer.msg("操作失败。");
            }
        });
    };

    //列表操作
    table.on('tool(userList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addUser(data);
        }else if(layEvent === 'pass'){
            audit(data.userId,1);
        }else if(layEvent === 'notPass') {
            audit(data.userId,0);
        }else if(layEvent === 'init'){
            layer.confirm('确定重置用户'+data.username+'的密码为888888？',{icon:3, title:'提示信息'},function(index){
                $.get("/admin/sys/user/initPassword.do",{
                    userId : data.userId
                },function(data){
                    if(data.code==0){
                        tableIns.reload();
                        layer.close(index);
                    }else{
                        layer.msg("重置密码失败");
                        layer.close(index);
                    }
                })
            });
        }
    });

});
